package com.bbm.communitysystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.domain.Post;
import com.bbm.communitysystem.domain.bo.PostBo;
import com.bbm.communitysystem.domain.dto.post.PostDto;
import com.bbm.communitysystem.mapper.PostMapper;
import com.bbm.communitysystem.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.communitysystem.tools.DemoDate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author transitory
 * @since 2025-02-20
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {

    @Override
    public PostDto putPostInfo(PostBo postBo) {
        boolean update;
        if (postBo == null) {
            return null;
        }

        if (postBo.getStatus() != 2) {
            String now = DemoDate.getNow();
            Post byId = getById(postBo.getId());

            if (byId.getStatus() == 2) {
                update = lambdaUpdate()
                        .set(postBo.getContent() != null && !postBo.getContent().isEmpty(), Post::getContent, postBo.getContent())
                        .set(postBo.getType() != null && !postBo.getType().isEmpty(), Post::getType, postBo.getType())
                        .set(postBo.getOssId() != null && !postBo.getOssId().isEmpty(), Post::getOssId, postBo.getOssId())

                        .set(postBo.getAddress() != null &&  !postBo.getAddress().isEmpty(), Post::getAddress, postBo.getAddress())

                        .set(postBo.getLatitude() != null, Post::getLatitude, postBo.getLatitude())
                        .set(postBo.getLikes() != null, Post::getLikes, postBo.getLikes())
                        .set(postBo.getInterest() != null, Post::getInterest, postBo.getInterest())
                        .set(postBo.getComment() != null, Post::getComment, postBo.getComment())
                        .set(postBo.getForward() != null, Post::getForward, postBo.getForward())
                        .set(postBo.getStatus() != null, Post::getStatus, postBo.getStatus())

                        .set(Post::getPublicTime, now)
                        .set(Post::getUpdateBy, postBo.getUpdateBy())
                        .set(Post::getUpdateTime, System.currentTimeMillis() + "")
                        .eq(Post::getId, postBo.getId())
                        .update();

            }

        } else {
            update = lambdaUpdate()
                    .set(postBo.getContent() != null && !postBo.getContent().isEmpty(), Post::getContent, postBo.getContent())
                    .set(postBo.getType() != null &&  !postBo.getType().isEmpty(), Post::getType, postBo.getType())
                    .set(postBo.getOssId() != null &&   !postBo.getOssId().isEmpty(), Post::getOssId, postBo.getOssId())

                    .set(postBo.getAddress() != null &&!postBo.getAddress().isEmpty(), Post::getAddress, postBo.getAddress())

                    .set(postBo.getLatitude() != null, Post::getLatitude, postBo.getLatitude())
                    .set(postBo.getLikes() != null, Post::getLikes, postBo.getLikes())
                    .set(postBo.getInterest() != null, Post::getInterest, postBo.getInterest())
                    .set(postBo.getComment() != null, Post::getComment, postBo.getComment())
                    .set(postBo.getForward() != null, Post::getForward, postBo.getForward())
                    .set(postBo.getStatus() != null, Post::getStatus, postBo.getStatus())

                    .set(Post::getUpdateBy, postBo.getUpdateBy())
                    .set(Post::getUpdateTime, System.currentTimeMillis() + "")
                    .eq(Post::getId, postBo.getId())
                    .update();
        }

        Post byId = getById(postBo.getId());

        return BeanUtil.copyProperties(byId, PostDto.class);
    }
}
